<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : message.class.php   FILE_PATH : \\classes\application\message.class.php
 * ....站内发送短消息类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Tue Nov 13 10:19:51 CST 2007
 */

class MessageClass extends FrameWork {
	/**
	 * 发送短消息
	 *
	 * @param var[] $input_param
	 * @return boolen
	 */
	function addMessage($input_param){
		$arr = array();
		$arr["member_name"] = $input_param["member_name"];
		$arr["receive_name"] = $input_param["txtReceive_name"];
		$arr["title"] = $input_param["txtTitle"];
		$arr["content"] = Common::replacebr($input_param["txtContent"]);
		if ($input_param["txtEndDate"] != ""){
			$arr["end_date"] = $input_param["txtEndDate"];
		}
		if ($input_param["rdoIsallowdel"] != ""){
			$arr["isallowdel"] = $input_param["rdoIsallowdel"];
		}
		$arr["send_time"] = time();
		if ($input_param["chkIssave"] != ""){
			$arr["issave"] = $input_param["chkIssave"];
		}
		$insert_rs = $GLOBALS['db']->InsertRow($arr, 'message', 'message_id');
		return $insert_rs;
	}

	/**
	 * 得到短消息列表
	 *
	 * @param var[] $condition  条件数组，一维数组，格式为：$arr[键名] = 要查找的值
	 * @param object $obj_page  分页对象，根据它得出当前需要的数据数组
	 * @param string $field   所需要的字段
	 * @param $table_name 表名
	 * @return var[]  二维数组，格式为$result[行号][列名]
	 */
	function getMessage($condition,&$obj_page,$field = "receive_name,title,message_id,member_name,send_time,isnew,isallowdel",$table_name='message'){
		//得到条件语句
		$condition_str = $this->getCondition($condition);
		if ($condition['genre'] == "receive"){
			//删除过期的固定短消息
			$this->_delOutDateMessage(array('receive_name'=>$condition['member_name'],'end_date'=>time()));
		}
		//得到站内短消息表中符合要求的数据
		$message_array = $GLOBALS['db']->GetList($condition_str,$obj_page,$table_name,$field,"send_time desc");
		return $message_array;
	}

	/**
	 * 删除过期的短消息
	 *
	 * @param string $condition  删除条件数组
	 * @return var[]
	 */
	function _delOutDateMessage($condition){
		//得到条件语句
		$condition_str = $this->getCondition($condition);
		return $GLOBALS['db']->DeleOneRow("",'message',"",$condition_str);
	}

	/**
	 * 删除留言
	 *
	 * @param string $input_id  要删除的ID或ID数组集合
	 * @param string $condition   删除的条件，必须同时满足条件才能删除
	 * @param $table_name 表名
	 * @param $id 需要匹配的字段名
	 * @return boolen
	 */
	function delMessage($input_id,$condition = "",$table_name='message',$id='message_id'){
		//得到条件语句
		$condition_str = $this->getCondition($condition);
		if ($input_id != ''){
			return $GLOBALS['db']->DelRow($input_id,$table_name,$id,$condition_str);
		}else {
			return false;
		}
	}

	/**
	 * 得到某条短消息内容
	 *
	 * @param var[] $condition  条件数组
	 * @param string $field  所需要的字段
	 * @param $table_name 表名
	 * @return var[] 一维数组
	 */
	function getOneMessage($condition,$field = "title,member_name,content,message_id,send_time,isnew",$table_name='message'){
		//得到条件语句
		$condition_str = $this->getCondition($condition);
		//得到某条留言信息
		return $message_array = $GLOBALS['db']->GetTheRow($condition_str,$table_name,$field);
	}
	
	/**
	 * 短信息改变状态
	 * @author workflow Develop Team
	 * @param  $message_id 短信ID
	 * @return bool
	 */
	function updateMessageState($message_id,$update_array){
		$GLOBALS['db']->UpdateRow($message_id,$update_array,'message','message_id');
		return true;	
	}
	
	
	/**
	 * 单一发送系统短消息
	 * @author workflow Develop Team
	 * @param  $input_param 短信息参数
	 * @return $message_system_id 信息ID
	 */
	function addOneSystemMessage($input_param){
		
		$arr = array();
		$time = time();
		$arr["title"] = $input_param["title"];
		$arr["content"] = $input_param["content"];
		$arr["send_time"] = $time;
		$arr["send_object"] = $input_param["send_object"];
		$arr["system_receive_name"] = $input_param["system_receive_name"];		
		
		$insert_rs = $GLOBALS['db']->InsertRow($arr, 'message_system', 'message_system_id');
		/*取最新插入的消息ID*/
		$message_system_id = $GLOBALS['db']->GetOneRow($time,'message_system','send_time','message_system_id');
		return $message_system_id['message_system_id'];
	}
	
	/**
	 * 逻辑删除系统群发信息
	 * @author workflow Develop Team
	 * @param  $input_id 短信息ID
	 * @return bool
	 */
	function delSystemMessage($input_id){
		
		$update_array = array();
		$update_array['state'] = 1;
		
		foreach ($input_id as $key => $v){
			$GLOBALS['db']->UpdateRow($v,$update_array,'message_system','message_system_id');
		}
		return true;
	}
	
	/**
	 * 取未读信息数目
	 * @author workflow Develop Team
	 * @param  $condition查找条件
	 * @return int
	 */
	function getNewMessageNum($condition){
		$array = $this->getMessage($condition,&$obj_page,'count(*) as count','message');
		return $array[0]['count'];
	}
	
	/**
	 * 将条件数组组合为SQL语句的条件部分
	 *
	 * @param var[] $conditon_array
	 * @return string
	 */
	function getCondition($conditon_array){
		if (trim($conditon_array["member_name"]) != ""){
			if ($conditon_array["genre"] == "send"){
				//查询发送人
				$condition_sql .= " and message.member_name ='" . $conditon_array["member_name"] . "' and message.issave='1'";
			}else if($conditon_array["genre"] == "receive"){
				//查询接收人
				$condition_sql .= " and message.receive_name ='" . $conditon_array["member_name"] . "'";
			}
		}
		//查询接收人
		if (trim($conditon_array["receive_name"]) != ""){
			$condition_sql .= " and message.receive_name ='" . $conditon_array["receive_name"] . "'";
		}
		//查询发送人
		if (trim($conditon_array["send_name"]) != ""){
			$condition_sql .= " and message.member_name ='" . $conditon_array["send_name"] . "'";
		}
		//查询留言ID
		if (trim($conditon_array["message_id"]) != ""){
			if (is_array($conditon_array["message_id"])){
				foreach ($conditon_array["message_id"] as $v){
					$condition_sql .= "and message.message_id ='" . $v . "'";
				}
			}else {
				$condition_sql .= " and message.message_id ='" . $conditon_array["message_id"] . "'";
			}
		}
		//此ID以外的留言ID
		if (trim($conditon_array["no_message_id"]) != ""){
			$condition_sql .= " and message.message_id <>'" . $conditon_array["message_id"] . "'";
		}
		//查询留言结束时间
		if (trim($conditon_array["end_date"]) != ""){
			$condition_sql .= " and message.end_date <'" . $conditon_array["end_date"] . "'";
		}
		//查询留言是否允许删除
		if (trim($conditon_array["isallowdel"]) != ""){
			$condition_sql .= " and message.isallowdel ='" . $conditon_array["isallowdel"] . "'";
		}
		//查询留言是否未读
		if (trim($conditon_array["isnew"]) != ""){
			$condition_sql .= " and message.isnew ='" . $conditon_array["isnew"] . "'";
		}
		//系统群发短信ID 系统后台 查看短信 和删除 短信
		if (trim($conditon_array["message_system_id"]) != ""){
			if (is_array($conditon_array["message_system_id"])){
				foreach ($conditon_array["message_system_id"] as $key => $v){
					if ($key == 0){
						$condition_sql .= " and message_system_id ='" . $v . "'";
					}else{
						$condition_sql .= " or message_system_id ='" . $v . "'";
					}
				}
			}else {
				$condition_sql .= " and message_system_id ='" . $conditon_array["message_system_id"] . "'";
			}
		}
		//系统群发短信状态
		if (trim($conditon_array["system_state"]) != ""){
			$condition_sql .= " and state ='" . $conditon_array["system_state"] . "'";
		}
		//排列顺序
		if (trim($conditon_array["order"]) == '1'){
			$condition_sql .= " order by send_time desc ";
		}elseif (trim($conditon_array["order"]) == '2'){
			$condition_sql .= " order by message.member_name,message.send_time desc ";
		}
		
		return $condition_sql;
	}
	
}
?>